package com.itboye.hutouben.activity.addressmanage;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.bean.MyAddressBean;
import com.itboye.hutouben.db.ProvinceActivity;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * 添加收货地址；修改收货地址
 */
public class AddressDetailActivity extends BaseActivity implements Observer{
    String type;
    ImageView icon_close;
    TextView save,add_shap_title_tv;
    EditText edit_name,edit_mobile,edit_youbian,edit_detail;
    ToggleButton toggle_moren;

    RelativeLayout address_quyu;//选择区域
    TextView address_sheng,address_shi,address_qu;//省市区

    UserPresenter userPresenter;
    String uid;
    String id;
    MyAddressBean bean;
    String provinceName,cityName,arerName;//从选择城市页面返回的省市区
    public static String ADDRESSEXIT = "AddressDetailActivity.exit";// 界面推出指令

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_detail);
        save.setVisibility(View.VISIBLE);
        uid=(String) SPUtils.get(this,null, Const.ID,"");
        id=this.getIntent().getStringExtra("id");
        userPresenter=new UserPresenter(this);

        init();
        // 接到广播结束页面
        IntentFilter filter1 = new IntentFilter(ADDRESSEXIT);
        registerReceiver(addressBroadcastReceiver, filter1);
    }
    private void init(){
        type = getIntent().getStringExtra("type");
        provinceName=getIntent().getStringExtra("provinceName");
        cityName=getIntent().getStringExtra("cityName");
        arerName=getIntent().getStringExtra("arerName");
        bean=(MyAddressBean)getIntent().getSerializableExtra("bean");
        if(!type.equals("edit")) {
            add_shap_title_tv.setText(getString(R.string.addAddress));
            address_sheng.setText(provinceName);
            address_shi.setText(cityName);
            address_qu.setText(arerName);
            edit_name.setText(getIntent().getStringExtra("name"));
            edit_mobile.setText(getIntent().getStringExtra("mobile"));
            edit_youbian.setText(getIntent().getStringExtra("youbian"));
            edit_detail.setText(getIntent().getStringExtra("street"));
            String def=getIntent().getStringExtra("default");
            if(def!=null){
                if(def.equals("1")){
                    toggle_moren.setChecked(true);
                }else{
                    toggle_moren.setChecked(false);
                }
            }

        }else{
            add_shap_title_tv.setText(getString(R.string.updateAddress));
            if(bean!=null){
                edit_name.setText(bean.getContactname());
                edit_mobile.setText(bean.getMobile());
                edit_youbian.setText(bean.getPostal_code()+"");
                address_sheng.setText(bean.getProvince());
                address_shi.setText(bean.getCity());
                address_qu.setText(bean.getArea());
                edit_detail.setText(bean.getDetailinfo());
                if(bean.getIs_default()==1){
                    toggle_moren.setChecked(true);
                }else{
                    toggle_moren.setChecked(false);
                }
            }else{
                address_sheng.setText(provinceName);
                address_shi.setText(cityName);
                address_qu.setText(arerName);
                edit_name.setText(getIntent().getStringExtra("name"));
                edit_mobile.setText(getIntent().getStringExtra("mobile"));
                edit_youbian.setText(getIntent().getStringExtra("youbian"));
                edit_detail.setText(getIntent().getStringExtra("street"));
                if((getIntent().getStringExtra("default").equals("1"))){
                    toggle_moren.setChecked(true);
                }else{
                    toggle_moren.setChecked(false);
                }
            }
        }
    }
    BroadcastReceiver addressBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            AddressDetailActivity.this.finish();
        };
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(addressBroadcastReceiver);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            String  province= bundle.getString("provinceName");
            address_sheng.setText(province);
            address_shi.setText("");
            address_qu.setText("");
        }
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        String  name=edit_name.getText().toString().trim();
        String  mobile=edit_mobile.getText().toString().trim();
        String  youbian=edit_youbian.getText().toString().trim();
        String  privince=address_sheng.getText().toString().trim();
        String  city=address_shi.getText().toString().trim();
        String  area=address_qu.getText().toString().trim();
        String  detail=edit_detail.getText().toString().trim();
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.address_quyu:
                Intent intent=new Intent(this,ProvinceActivity.class);
                intent.putExtra("type",type);
                intent.putExtra("name",name);
                intent.putExtra("mobile",mobile);
                intent.putExtra("youbian",youbian+"");
                intent.putExtra("street",detail);
                intent.putExtra("default",toggle_moren.isChecked()?1+"":0+"");
                intent.putExtra("id",id);
                startActivityForResult(intent, 0);
                break;
            case R.id.save:
                if(name.equals("")){
                    ByAlert.alert("请输入姓名");
                    return;
                }
                if(mobile.equals("")){
                    ByAlert.alert("请输入手机号");
                    return;
                }
                if(youbian.equals("")){
                    ByAlert.alert("请输入邮编");
                    return;
                }
                if(detail.equals("")){
                    ByAlert.alert("请输入详细地址");
                    return;
                }
                if (type.equals("edit")) {//修改地址
                    showProgressDialog("更新中,请稍后", true);
                    userPresenter.updateAddress(IsUtilUid.sId(),id,uid,name,mobile,youbian,privince,city,area,detail,toggle_moren.isChecked()?1+"":0+"","中国","1");
                } else {//添加地址
                    showProgressDialog("添加中,请稍后", true);
                    userPresenter.addressAdd(IsUtilUid.sId(),uid,name,mobile,youbian,privince,city,area,detail,toggle_moren.isChecked()?1+"":0+"","中国","1");
                }
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity=handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if(entity.getEventType()==UserPresenter.add_address_success) {
                ByAlert.alert("添加成功");
                finish();
                Intent intent=new Intent("addaddress");
                sendBroadcast(intent);
            }
            if(entity.getEventType()==UserPresenter.add_address_fail) {
                ByAlert.alert(entity.getData());
            }
            if(entity.getEventType()==UserPresenter.update_address_success) {
                ByAlert.alert(entity.getData());
                finish();
                Intent intent=new Intent("addaddress");
                sendBroadcast(intent);
            }
            if(entity.getEventType()==UserPresenter.update_address_fail) {
                ByAlert.alert(entity.getData());
            }
        }

    }
}
